package com.alphaking.Jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alphaking.bean.Share;
import com.alphaking.bean.Upvote;
import com.mysql.jdbc.PreparedStatement;

public class JdbcShare {

	// 获得当前数据库的连接
	private static Connection con = JdbcUtils.getCon();

	// 删除
	private static String deleteSql = "delete from share where shareid = ?";
	// 更新的语句
	private static String sqlUpdate = "update share set sharecontent=?,imageaddress=? where shareid=?";
	// 分页查询
	private static String queryShare = "select * from share  where shareid = ?";

	/**
	 * 增添分享
	 * 
	 * @param share
	 * @param account
	 */
	public static void add(Share share, String account) {

		// 插入的语句
		String sqlInsert = "insert into share" + "(user_account,sharecontent,imageaddress, sharedate) "
				+ "values(?,?,?,? )";
		try {

			PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(sqlInsert);

			pstmt.setString(1, account);
			pstmt.setString(2, share.getShareContent());

			List<String> image = share.getImageAddress();

			StringBuilder imageAddress = new StringBuilder();
			for (int i = 0; i < image.size(); i++) {

				imageAddress.append(image.get(i));
				if (i < image.size() - 1) {
					imageAddress.append(',');
				}

			}
			pstmt.setString(3, imageAddress.toString());
			pstmt.setString(4, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

			pstmt.execute();
			pstmt.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 删除数据
	 * 
	 * @param shareId
	 */
	public static void delete(int shareId) {

		try {
			PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(deleteSql);
			pstmt.setInt(1, shareId);
			pstmt.execute();
			pstmt.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 更新分享的内容
	 * 
	 * @param share
	 */
	public static void update(Share share) {

		try {
			PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(sqlUpdate);
			pstmt.setString(1, share.getShareContent());
			List<String> image = share.getImageAddress();
			StringBuilder imageAddress = new StringBuilder();
			for (int i = 0; i < image.size(); i++) {

				imageAddress.append(image.get(i));
				if (i < image.size() - 1) {
					imageAddress.append(',');
				}
			}
			pstmt.setString(2, imageAddress.toString());
			pstmt.setInt(3, share.getShareId());
			pstmt.execute();
			pstmt.close();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 查询特定的一条分享
	 * 
	 * @param shareId
	 * @return
	 */
	public static Share query(int shareid) {

		Share share = new Share();

		try {

			PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(queryShare);
			pstmt.setInt(1, shareid);
			ResultSet rst = pstmt.executeQuery();
			if (rst.next()) {
				share.setShareId(rst.getInt("shareid"));
				share.setShareContent(rst.getString("sharecontent"));
				String[] images = rst.getString("imageaddress").split(",");
				List<String> imageadress = new ArrayList<String>();
				for (int i = 0; i < images.length; i++) {
					imageadress.add(images[i]);
				}
				share.setImageAddress(imageadress);
				share.setNumberOfcomments(rst.getInt("numcomments"));
				share.setNumberOfupvotes(rst.getInt("numUpvotes"));
				share.setComments(JdbcComments.getComments(shareid));
				share.setShareDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(rst.getDate("sharedate").toString()));
				List<Upvote> upvotes = JdbcUpvote.getUpvote(shareid);
				share.setUpvotes(upvotes);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return share;
	}

	public static List<Share> getUserShare(String account) {

		List<Share> listShares = new ArrayList<Share>();

		return listShares;
	}

}
